package com.emellend.api11.graphics;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.emellend.api11.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;

public class BitmapDecode extends GraphicsActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}
	private static class SampleView extends View {
		private Bitmap mBitmap;
        private Bitmap mBitmap2;
        private Bitmap mBitmap3;
        private Bitmap mBitmap4;
        private Drawable mDrawable;
        
        private Movie mMovie;
        private long mMovieStart;
        
      //Set to false to use decodeByteArray
        private static final boolean DECODE_STREAM = false;
        
        private static byte[] streamToBytes(InputStream is){
        	ByteArrayOutputStream os=new ByteArrayOutputStream(1024);
        	byte[] buffer=new byte[1024];
        	int len;
        	try {
				while ((len=is.read(buffer))>=0) {
					os.write(buffer, 0, len);
				}
			} catch (IOException e) {
				Log.e("test", e.getMessage());
			}
        	return os.toByteArray();
        }
        
		public SampleView(Context context) {
			super(context);
			setFocusable(true);
			InputStream is;
			is=context.getResources().openRawResource(R.drawable.beach);
			
			BitmapFactory.Options opts=new BitmapFactory.Options();
			Bitmap bm;
			
			opts.inJustDecodeBounds=false;
			opts.inSampleSize=4;
			bm=BitmapFactory.decodeStream(is, null, opts);
			mBitmap=bm;
			is=context.getResources().openRawResource(R.drawable.frog);
			mBitmap2=BitmapFactory.decodeStream(is);
			
			int w = mBitmap2.getWidth();
			int h = mBitmap2.getHeight();
			int[] pixels=new int[w*h];
			mBitmap2.getPixels(pixels, 0, w, 0, 0, w, h);
			
			mBitmap3=Bitmap.createBitmap(pixels, 0, w, w, h, Bitmap.Config.ARGB_8888);
			mBitmap4=Bitmap.createBitmap(pixels, 0, w, w, h, Bitmap.Config.ARGB_4444);
			
			mDrawable=context.getResources().getDrawable(R.drawable.button);
			mDrawable.setBounds(150, 20, 300, 100);
			
			is=context.getResources().openRawResource(R.drawable.animated_gif);
			if (DECODE_STREAM) {
				mMovie=Movie.decodeStream(is);
			} else {
				byte[] array=streamToBytes(is);
				mMovie=Movie.decodeByteArray(array, 0, array.length);
			}
		}
		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(0xFFCCCCCC);
			
			
			canvas.drawBitmap(mBitmap, 10, 10, null);
			canvas.drawBitmap(mBitmap2, 10, 170, null);
			canvas.drawBitmap(mBitmap3, 110, 170, null);
			canvas.drawBitmap(mBitmap4, 210, 170, null);
			
			mDrawable.draw(canvas);
			
			long now = SystemClock.uptimeMillis();
			if (mMovieStart==0) {
				mMovieStart=now;
			}
			if (mMovie!=null) {
				int duration = mMovie.duration();
				if (duration==0) {
					duration=1000;
				}
				int relTime=(int) ((now-mMovieStart)%duration);
				mMovie.setTime(relTime);
				mMovie.draw(canvas, getWidth()-mMovie.width(), getHeight()-mMovie.height());
				invalidate();
			}
		}
	}
}
